package com.lagou.rabbitmq.direct;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeoutException;

public class HelloGetConsumer {

    public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException, URISyntaxException, IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        //指定协议 amqp://
        //指定用户名 root
        //指定密码 123456
        //指定host 47.115.74.219
        //指定端口号 5672
        //指定虚拟主机 %2f
        factory.setUri("amqp://root:123456@47.115.74.219:5672/%2f");

        Connection connection = factory.newConnection();

        Channel channel = connection.createChannel();

        //指定从哪个消费者消费消息
        //指定是否自动确认消息 true表示自动确认
        GetResponse response = channel.basicGet("queue.biz", true);

        //获取消息体
        byte[] body = response.getBody();
        System.out.println(new String(body, StandardCharsets.UTF_8));

        //获取消息头
        //AMQP.BasicProperties props = response.getProps();

        //关闭通道
        channel.close();
        //关闭连接
        connection.close();
    }

}
